﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.Common;
using yncTech.BusinessObjects;

public partial class Admin_Fin_Fin005 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        VLendBoxCollection lendBoxes = new VLendBoxCollection();

        lendBoxes.Query.Where(lendBoxes.Query.CreatedDttm >= txtStartDate.Value && lendBoxes.Query.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));

        if ( LoginUserInfo.IsAuthenticated && !LoginUserInfo.CurrentUser.IsAdmin)
        {
            lendBoxes.Query.Where(lendBoxes.Query.ThirdUnitID == LoginUserInfo.CurrentUser.ThirdUnitID);
        }

        lendBoxes.Query.OrderBy(lendBoxes.Query.ActReturnDate.Ascending, lendBoxes.Query.CreatedDttm.Descending);

        pageGrid.DataSource = lendBoxes.Query.LoadDataTable();
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");

    }

    protected string GetActionUrl(object obj,object rowView,int boxes)
    {
        int? status = obj as int?;

        if (status.HasValue)
        {
            if (status.Value == (int)LendStatus.Pending)
            {
                string addDoc = GetLink("添加凭证", "A", "Fin005b.aspx", rowView, 980, 580, false, VLendBoxMetadata.ColumnNames.LendID);
                string conf = "";
                if (boxes > 0)
                {
                    conf = GetConfirmLink("确认借出", "C", "fin005a.aspx", rowView, "您确定要借出吗?", VLendBoxMetadata.ColumnNames.LendID) + "   ";
                }
                return addDoc+"   "+ conf + GetUpdateLink("fin005a.aspx", rowView, 660, 560, VLendBoxMetadata.ColumnNames.LendID) + "   " + GetDeleteLink("fin005a.aspx", rowView, VLendBoxMetadata.ColumnNames.LendID);
            }
            if (status.Value == (int)LendStatus.Lent)
            {
                return GetConfirmLink("确认归还", "B", "fin005a.aspx", rowView, "您确认文件已归还吗?", VLendBoxMetadata.ColumnNames.LendID);
            }

            
        }

        return "";
    }
}